package com.shop.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shop.entity.buyer;
import com.shop.service.buyerserviceI;
import com.shop.service.buyerserviceImpl;

/**
 * Servlet implementation class buyerServlet
 */
@WebServlet("/buyerServlet")
public class buyerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public buyerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 将HTML传过来的字符设置为UTF-8 防止出现乱码
		request.setCharacterEncoding("UTF-8");

		// 设置用户注册时的默认角色
		String role = "普通用户";
		String user = request.getParameter("user");
		String pwd = request.getParameter("pwd");
		String phone = request.getParameter("phone");
		System.out.println(user + ":" + pwd + ":" + phone);
		// 创建业务层对象
		buyerserviceI bsi = new buyerserviceImpl();
		buyer buyer = new buyer();
		buyer.setUid(user);
		buyer.setPassword(pwd);
		buyer.setPhone(phone);
		buyer.setRole(role);
		try {
			if (bsi.register(buyer)) {
				response.sendRedirect("../1-2-3/test1/login.html");
			} 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
